package com.zycfc.zsf.boot.mybatis.generator.codegen.db.impl;

import com.zycfc.zsf.boot.mybatis.generator.codegen.exception.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.db.*;
import java.sql.*;
import java.util.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.model.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.util.*;
import com.zycfc.zsf.boot.util.text.*;

public class OracleHelper implements IDbHelper
{
    private String sqlPk;
    private String sqlTableComment;
    private String sqlColumn;
    private String sqlAllTables;
    private String url;
    private String username;
    private String password;
    
    public OracleHelper() throws CodegenException {
        this.sqlPk = "select column_name from user_constraints c,user_cons_columns col where c.constraint_name=col.constraint_name and c.constraint_type='P' and c.table_name='%s'";
        this.sqlTableComment = "select * from user_tab_comments  where table_type='TABLE' AND table_name ='%s'";
        this.sqlColumn = "select    A.column_name NAME,A.data_type TYPENAME,A.data_length LENGTH,A.data_precision PRECISION,    A.Data_Scale SCALE,A.Data_default, A.NULLABLE, B.comments DESCRIPTION  from  user_tab_columns A,user_col_comments B where a.COLUMN_NAME=b.column_name and    A.Table_Name = B.Table_Name and A.Table_Name='%s' order by A.column_id";
        this.sqlAllTables = "select table_name from user_tables where status='VALID'";
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        }
        catch (ClassNotFoundException e) {
            throw new CodegenException("\u627e\u4e0d\u5230oracle\u9a71\u52a8!", e);
        }
    }
    
    @Override
    public void setUrl(final String url, final String username, final String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }
    
    private List<ColumnModel> getColumnsByTable(String tableName) throws CodegenException {
        tableName = tableName.toUpperCase();
        final DaoHelper<ColumnModel> dao = new DaoHelper<ColumnModel>(this.url, this.username, this.password);
        final String sql = String.format(this.sqlColumn, tableName);
        final List<ColumnModel> list = dao.queryForList(sql, new OracleMapCmd());
        return list;
    }
    
    private String getTableComment(String tableName) throws CodegenException {
        tableName = tableName.toUpperCase();
        final String sql = String.format(this.sqlTableComment, tableName);
        final DaoHelper<String> dao = new DaoHelper<String>(this.url, this.username, this.password);
        String comment = dao.queryForObject(sql, new MapCmd<String>() {
            @Override
            public String getObjecFromRs(final ResultSet rs) throws SQLException {
                return rs.getString("COMMENTS");
            }
        });
        if (comment == null) {
            comment = tableName;
        }
        final String[] aryComment = comment.split("\n");
        return aryComment[0];
    }
    
    private String getPk(String tableName) throws CodegenException {
        tableName = tableName.toUpperCase();
        final String sql = String.format(this.sqlPk, tableName);
        final DaoHelper<String> dao = new DaoHelper<String>(this.url, this.username, this.password);
        String pk = "";
        try {
            pk = dao.queryForObject(sql, new MapCmd<String>() {
                @Override
                public String getObjecFromRs(final ResultSet rs) throws SQLException {
                    return rs.getString("COLUMN_NAME");
                }
            });
        }
        catch (Exception e) {
            throw new CodegenException("\u4ece\u8868\u4e2d\u53d6\u5f97\u4e3b\u952e\u51fa\u9519\uff0c\u8bf7\u68c0\u67e5\u8868\u662f\u5426\u8bbe\u7f6e\u4e3b\u952e");
        }
        return pk;
    }
    
    private void setPk(final List<ColumnModel> list, final String pk) {
        for (final ColumnModel model : list) {
            if (pk.toLowerCase().equals(model.getColumnName().toLowerCase())) {
                model.setIsPK(true);
            }
        }
    }
    
    @Override
    public TableModel getByTable(String tableName) throws CodegenException {
        tableName = tableName.toUpperCase();
        final String tabComment = this.getTableComment(tableName);
        final String pk = this.getPk(tableName);
        final TableModel tableModel = new TableModel();
        tableModel.setTableName(tableName);
        tableModel.setTabComment(tabComment);
        final List<ColumnModel> list = this.getColumnsByTable(tableName);
        if (StringUtil.isNotEmpty(pk)) {
            this.setPk(list, pk);
        }
        tableModel.setColumnList(list);
        return tableModel;
    }
    
    @Override
    public List<String> getAllTable(final String tableName) throws CodegenException {
        final DaoHelper<String> dao = new DaoHelper<String>(this.url, this.username, this.password);
        if (!StringUtils.isEmpty(tableName)) {
            this.sqlAllTables = this.sqlAllTables + " AND Table_Name like '%" + tableName + "%'";
        }
        return dao.queryForList(this.sqlAllTables, new MapCmd<String>() {
            @Override
            public String getObjecFromRs(final ResultSet rs) throws SQLException {
                return rs.getString("TABLE_NAME");
            }
        });
    }
    
    @Override
    public List<String> getAllTable() throws CodegenException {
        return this.getAllTable(null);
    }
}
